package com.ny.dao.impl;

import com.ny.dao.OrderDao;
import com.ny.entity.Order;
import com.ny.until.DbUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private DbUtil dbUtil = new DbUtil();

    @Override
    public int insertOrder(Order order) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "INSERT INTO orders(student_id, product_id, number, total_price) VALUES (?, ?, ?, ?)";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, order.getStudentId());
        prepareStatement.setInt(2, order.getProductId());
        prepareStatement.setInt(3, order.getNumber());
        prepareStatement.setDouble(4, order.getTotalPrice());
        return prepareStatement.executeUpdate();
    }
    public Order selectOrderById(int orderId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM orders WHERE order_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, orderId);
        ResultSet resultSet = prepareStatement.executeQuery();

        if (resultSet.next()) {
            Order order = new Order();
            order.setOrderId(resultSet.getInt("order_id"));
            order.setStudentId(resultSet.getInt("student_id"));
            order.setProductId(resultSet.getInt("product_id"));
            order.setNumber(resultSet.getInt("number"));
            order.setTotalPrice(resultSet.getDouble("total_price"));
            return order;
        }
        return null;
    }

    @Override
    public List<Order> selectOrdersByStudentId(int studentId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "SELECT * FROM orders WHERE student_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, studentId);
        ResultSet resultSet = prepareStatement.executeQuery();

        List<Order> orders = new ArrayList<>();
        while (resultSet.next()) {
            Order order = new Order();
            order.setOrderId(resultSet.getInt("order_id"));
            order.setStudentId(resultSet.getInt("student_id"));
            order.setProductId(resultSet.getInt("product_id"));
            order.setNumber(resultSet.getInt("number"));
            order.setTotalPrice(resultSet.getDouble("total_price"));
            orders.add(order);
        }
        return orders;
    }

    @Override
    public int deleteOrder(int orderId) throws SQLException {
        Connection connection = dbUtil.getCon();
        String sql = "DELETE FROM orders WHERE order_id = ?";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setInt(1, orderId);
        return prepareStatement.executeUpdate();
    }
}